<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入充电宝店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺评分" prop="shopScore">
        <el-input v-model="queryParams.shopScore" placeholder="请输入店铺评分" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品牌" prop="deviceBrand">
        <el-input v-model="queryParams.deviceBrand" placeholder="请输入充电宝品牌" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所在城市" prop="shopCity">
        <el-input v-model="queryParams.shopCity" placeholder="请输入所在城市" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺星级" prop="starName">
        <el-input v-model="queryParams.starName" placeholder="请输入充电宝店铺星级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商圈" prop="shopBusiness">
        <el-input v-model="queryParams.shopBusiness" placeholder="请输入商圈" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['memebr:memebr:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['memebr:memebr:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['memebr:memebr:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['memebr:memebr:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memebrList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="充电宝id" align="center" prop="id" /> -->
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="店铺地址" align="center" prop="shopAddress" />
      <el-table-column label="每小时单价" align="center" prop="productPrice" />
      <el-table-column label="店铺评分" align="center" prop="shopScore" />
      <el-table-column label="品牌" align="center" prop="deviceBrand" />
      <el-table-column label="所在城市" align="center" prop="shopCity" />
      <el-table-column label="店铺星级" align="center" prop="starName" />
      <el-table-column label="商圈" align="center" prop="shopBusiness" />
      <el-table-column label="纬度" align="center" prop="shopLatitude" />
      <el-table-column label="经度" align="center" prop="shopLongitude" />
      <el-table-column label="店铺图片" align="center" prop="shopPic" />
      <!-- <el-table-column label="所属租户" align="center" prop="tantId" /> -->
      <el-table-column label="同步状态" align="center" prop="syancStatus" />
      <!-- <el-table-column label="第三方物联网平台Id" align="center" prop="iotId" />
      <el-table-column label="所属产品标识" align="center" prop="productKey" /> -->
      <el-table-column label="在线状态 " align="center" prop="onlineStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSyanc(scope.row)"
            v-hasPermi="['memebr:memebr:edit']">同步</el-button>

            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['memebr:memebr:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['memebr:memebr:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改充电宝对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入充电宝店铺名称" />
        </el-form-item>
        <el-form-item label="商圈" prop="shopBusiness">
          <el-input v-model="form.shopBusiness" placeholder="请输入商圈" />
        </el-form-item>
        <el-form-item label="单价" prop="productPrice">
          <el-input v-model="form.productPrice" placeholder="请输入每小时单价" />
        </el-form-item>
        <el-form-item label="店铺评分" prop="shopScore">
          <el-input v-model="form.shopScore" placeholder="请输入充电宝店铺评分" />
        </el-form-item>
        <el-form-item label="品牌" prop="deviceBrand">
          <el-input v-model="form.deviceBrand" placeholder="请输入充电宝品牌" />
        </el-form-item>
        <el-form-item label="店铺地址" prop="shopAddress">
          <el-input v-model="form.shopAddress" placeholder="请输入充电宝店铺地址" />
        </el-form-item>
        <el-form-item >
           <baidu-map class="bmView" :center="{lng: 116.404, lat: 39.915}" :zoom="15" :scroll-wheel-zoom="true">
              <!-- <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list> -->
 <bm-local-search :keyword="keyword" :auto-viewport="true" :location="location"></bm-local-search>
           </baidu-map>
        </el-form-item>
        <el-form-item label="所在城市" prop="shopCity">
          <el-input v-model="form.shopCity" placeholder="请输入所在城市" />
        </el-form-item>
        <el-form-item label="店铺星级" prop="starName">
          <el-input v-model="form.starName" placeholder="请输入充电宝店铺星级" />

        </el-form-item>

        <el-form-item label="纬度" prop="shopLatitude">
          <el-input v-model="form.shopLatitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="经度" prop="shopLongitude">
          <el-input v-model="form.shopLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="充电宝店铺图片" prop="shopPic">
          <el-input v-model="form.shopPic" placeholder="请输入充电宝店铺图片" />
        </el-form-item>
        <!-- <el-form-item label="所属租户" prop="tantId">
          <el-input v-model="form.tantId" placeholder="请输入所属租户" />
        </el-form-item>
        <el-form-item label="第三方物联网平台Id" prop="iotId">
          <el-input v-model="form.iotId" placeholder="请输入第三方物联网平台Id" />
        </el-form-item>
        <el-form-item label="所属产品标识" prop="productKey">
          <el-input v-model="form.productKey" placeholder="请输入所属产品标识" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDevice,
    getDevice,
    delDevice,
    addDevice,
    updateDevice,
    syancDevice
  } from "@/api/device/index";

  export default {
    name: "Device",
    data() {
      return {
        location: {
          lng: 102.68906,
          lat: 25.049784
        }, // 指定地点的经纬度
        // 地图缩放比例
        zoom: 20,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // memebr表格数据
        memebrList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceCode: null,
          shopName: null,
          shopAddress: null,
          productPrice: null,
          shopScore: null,
          deviceBrand: null,
          shopCity: null,
          starName: null,
          shopBusiness: null,
          shopLatitude: null,
          shopLongitude: null,
          shopPic: null,
          tantId: null,
          syancStatus: null,
          iotId: null,
          productKey: null,
          onlineStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          shopName: [{
            required: true,
            message: "充电宝店铺名称不能为空",
            trigger: "blur"
          }],
          shopAddress: [{
            required: true,
            message: "充电宝店铺地址不能为空",
            trigger: "blur"
          }],
          productPrice: [{
            required: true,
            message: "每小时单价不能为空",
            trigger: "blur"
          }],
          shopScore: [{
            required: true,
            message: "充电宝店铺评分不能为空",
            trigger: "blur"
          }],
          deviceBrand: [{
            required: true,
            message: "充电宝品牌不能为空",
            trigger: "blur"
          }],
          shopCity: [{
            required: true,
            message: "所在城市不能为空",
            trigger: "blur"
          }],
          shopLatitude: [{
            required: true,
            message: "纬度不能为空",
            trigger: "blur"
          }],
          shopLongitude: [{
            required: true,
            message: "经度不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询memebr列表 */
      getList() {
        this.loading = true;
        listDevice(this.queryParams).then(response => {
          this.memebrList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          deviceCode: null,
          shopName: null,
          shopAddress: null,
          productPrice: null,
          shopScore: null,
          deviceBrand: null,
          shopCity: null,
          starName: null,
          shopBusiness: null,
          shopLatitude: null,
          shopLongitude: null,
          shopPic: null,
          tantId: null,
          syancStatus: null,
          iotId: null,
          productKey: null,
          onlineStatus: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加充电宝";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDevice(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改充电宝";
        });
      },

      handleSyanc(row){
        syancDevice(row).then(response => {
          this.$modal.msgSuccess("同步成功");
          this.getList();
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateDevice(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDevice(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除充电宝编号为"' + ids + '"的数据项？').then(function() {
          return delDevicess(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('memebr/device/export', {
          ...this.queryParams
        }, `device_${new Date().getTime()}.xlsx`)
      },



    }
  };
</script>



<style>
  .bmView {
    margin-top: 10px;
    height: 500px;
  }
</style>
